﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MovieCollectionManager.DataModel;
using System.Data.SqlClient;

namespace MovieCollectionManager
{
    public partial class AddForm : Form
    {
        public AddForm()
        {
            InitializeComponent();
        }

        private void buttonAddMovie_Click(object sender, EventArgs e)
        {
            if (textBoxTitle.Text == "")
            {
                MessageBox.Show("O filme deve possuir um título.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            using (var db = new MovieCollectionConteiner())
            {
                Movie newMovie = new Movie();
                newMovie.Title = textBoxTitle.Text;
                newMovie.Rate = Convert.ToDouble(numericUpDownRate.Value);
                newMovie.Type = comboBoxType.Text;
                newMovie.ReleaseDate = Convert.ToInt32(numericUpDownReleaseDate.Value);
                newMovie.Length = Convert.ToInt32(numericUpDownLength.Value);
                newMovie.Description = textBoxDescription.Text;
                newMovie.Lent = false;

                if (!string.IsNullOrEmpty(openFileDialog.FileName))
                {
                    byte[] imageBytes = GetBytesFromImage();
                    newMovie.BoxArt = imageBytes;
                }
                else
                {
                    pictureBoxArt.Image = null;
                    newMovie.BoxArt = new byte[0];
                }

                db.Movies.AddObject(newMovie);
                db.SaveChanges();
            }

            MessageBox.Show("Filme Adicionado!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ClearFields();
            textBoxTitle.Focus();
        }

        private byte[] GetBytesFromImage()
        {
            pictureBoxArt.Image = null;
            FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open);
            BinaryReader binaryReader = new BinaryReader(fileStream);
            byte[] imageBytes = binaryReader.ReadBytes(Convert.ToInt32(fileStream.Length));
            fileStream.Close();
            binaryReader.Close();
            return imageBytes;
        }

        private void ClearFields()
        {
            textBoxTitle.Text = "";
            numericUpDownRate.Value = 0;
            comboBoxType.SelectedIndex = 0;
            numericUpDownReleaseDate.Value = 2000;
            numericUpDownLength.Value = 120;
            textBoxDescription.Text = "";
            pictureBoxArt.Image = null;
        }

        private void pictureBoxArt_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.ShowDialog();
                if ((!string.IsNullOrEmpty(openFileDialog.FileName)))
                {
                    FileStream fileStream = new System.IO.FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
                    pictureBoxArt.Image = Image.FromStream(fileStream);
                    fileStream.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Imagem não é válida", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                openFileDialog.FileName = "";
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddForm_Load(object sender, EventArgs e)
        {
            comboBoxType.SelectedIndex = 0;
        }
    }
}
